<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统设置
            <small>角色管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>角色管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="角色名" align="center"></el-table-column>
                <el-table-column prop="keyword" label="角色关键词" align="center"></el-table-column>
                <el-table-column prop="description" label="角色描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="butT" @click="handleCreate(scope.row)">添加权限</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="添加权限" :visible.sync="dialogFormVisible">
                    <!--新增表单-->
                    <div class="checkScrol">
                        <table class="datatable">
                            <thead>
                            <tr>
                                <th>序号</th>
                                <th>权限名</th>
                                <th>权限关键字</th>
                                <th>权限描述</th>
                                <th>选择</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr v-for="c in tableData">
                                <td><label :for="c.id">{{c.id}}</label></td>
                                <td><label :for="c.id">{{c.name}}</label></td>
                                <td><label :for="c.id">{{c.keyword}}</label></td>
                                <td><label :for="c.id">{{c.description}}</label></td>
                                <td>
                                    <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                </td>
                            </tr>
                            </tbody>
                        </table>
                    </div>

                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>

                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            tableData:[],//添加表单中对应的检查项列表数据
            permissionIds:[],//新增表单中权限对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            id :null
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //添加
            handleAdd() {
                //发送ajax请求，将表单数据提交到后台进行处理
                axios.post("/role/add.do?permissionIds="+this.permissionIds+"&id="+this.id).then((res)=>{
                    //关闭窗口
                    this.dialogFormVisible = false;
                if (res.data.flag){
                    //操作成功
                    this.$message({
                        type:'success',
                        message:res.data.message
                    });
                }else {
                    //操作失败
                    this.$message.error(res.data.message);
                }
            }).finally(()=>{
                    this.findPage();
            });

            },
            //分页查询
            findPage() {
                var param = {
                    currentPage: this.pagination.currentPage,//页码
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/role/findPage.do", param).then((res) => {
                    this.dataList = res.data.rows;
                    this.pagination.total = res.data.total;
                });
            },
            // 重置表单
            resetForm() {
                //数组用[],对象(json对象)用{}
                this.formData = {};
            },
            // 弹出添加窗口
            handleCreate(row) {
                this.dialogFormVisible = true;
                this.id = row.id;
                this.resetForm();
                //发送axios请求，获取所有的权限信息
                axios.get("/role/findAll.do").then((res)=>{
                    if(res.data.flag){
                        this.tableData = res.data.data;
                        //发送ajax请求，根据角色id查询需要勾选的权限id
                        axios.get("/role/findPermissionIdsByRoleId.do?id="+row.id).then((res)=>{
                            if(res.data.flag){
                                this.permissionIds=res.data.data;
                            }else{
                                this.$message.error(res.data.message);
                            }
                        });
                    }else{
                        this.$message.error(res.data.message);
                    }
                });
            },

            //切换页码
            handleCurrentChange(currentPage) {
                //点击页码后分页查询
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            }
        }
    })
</script>
</html>